<template>
	<view class="factory-info-page">
    <TopBar
        left-icon="/static/user.png"
        scan-icon="/static/saoyisao.png"
        menu-icon="/static/menu.png"
        @menu="toggleMenu"
    />

        <scroll-view scroll-y class="form-scroll">
            <view class="back-row" @click="goBack">
                <image class="back-arrow-icon" src="/static/left.png" mode="aspectFit"></image>
            </view>
			<view class="header">
				<text class="title">Factory Profile</text>
				<text class="subtitle">Section 4. Materials & Processes</text>
			</view>

            <!-- Plant Fibers -->
            <text class="section-title">Plant Fibers</text>
            <view class="section">
                <view class="checkbox-list">
                    <view class="checkbox-item" v-for="item in plantFibers" :key="item.name" @click="toggleCheckbox('plantFibers', item.name)">
                        <view class="item-content">
                            <text class="bullet">•</text>
                            <text class="checkbox-label">{{ item.name }}</text>
                        </view>
                        <view class="checkbox" :class="{ checked: item.checked }">
                            <text class="checkmark" v-if="item.checked">✓</text>
                        </view>
                    </view>
                </view>
            </view>

            <!-- Animal Fibers -->
            <text class="section-title">Animal Fibers</text>
            <view class="section">
                <view class="checkbox-list">
                    <view class="checkbox-item" v-for="item in animalFibers" :key="item.name" @click="toggleCheckbox('animalFibers', item.name)">
                        <view class="item-content">
                            <text class="bullet">•</text>
                            <text class="checkbox-label">{{ item.name }}</text>
                        </view>
                        <view class="checkbox" :class="{ checked: item.checked }">
                            <text class="checkmark" v-if="item.checked">✓</text>
                        </view>
                    </view>
                </view>
            </view>

            <!-- Regenerated Cellulosic Fibers -->
            <text class="section-title">Regenerated Cellulosic Fibers</text>
            <view class="section">
                <view class="checkbox-list">
                    <view class="checkbox-item" v-for="item in regeneratedFibers" :key="item.name" @click="toggleCheckbox('regeneratedFibers', item.name)">
                        <view class="item-content">
                            <text class="bullet">•</text>
                            <text class="checkbox-label">{{ item.name }}</text>
                        </view>
                        <view class="checkbox" :class="{ checked: item.checked }">
                            <text class="checkmark" v-if="item.checked">✓</text>
                        </view>
                    </view>
                </view>
            </view>

            <!-- Synthetic Fibers -->
            <text class="section-title">Synthetic Fibers</text>
            <view class="section">
                <view class="checkbox-list">
                    <view class="checkbox-item" v-for="item in syntheticFibers" :key="item.name" @click="toggleCheckbox('syntheticFibers', item.name)">
                        <view class="item-content">
                            <text class="bullet">•</text>
                            <text class="checkbox-label">{{ item.name }}</text>
                        </view>
                        <view class="checkbox" :class="{ checked: item.checked }">
                            <text class="checkmark" v-if="item.checked">✓</text>
                        </view>
                    </view>
                </view>
            </view>

            <!-- Fillings & Insulation -->
            <text class="section-title">Fillings & Insulation</text>
            <view class="section">
                <view class="checkbox-list">
                    <view class="checkbox-item" v-for="item in fillings" :key="item.name" @click="toggleCheckbox('fillings', item.name)">
                        <view class="item-content">
                            <text class="bullet">•</text>
                            <text class="checkbox-label">{{ item.name }}</text>
                        </view>
                        <view class="checkbox" :class="{ checked: item.checked }">
                            <text class="checkmark" v-if="item.checked">✓</text>
                        </view>
                    </view>
                </view>
            </view>

            <!-- Functional / Special Materials -->
            <text class="section-title">Functional / Special Materials</text>
            <view class="section">
                <view class="checkbox-list">
                    <view class="checkbox-item" v-for="item in functionalMaterials" :key="item.name" @click="toggleCheckbox('functionalMaterials', item.name)">
                        <view class="item-content">
                            <text class="bullet">•</text>
                            <text class="checkbox-label">{{ item.name }}</text>
                        </view>
                        <view class="checkbox" :class="{ checked: item.checked }">
                            <text class="checkmark" v-if="item.checked">✓</text>
                        </view>
                    </view>
                </view>
            </view>

            <!-- Toggle Options -->
            <view class="toggle-section">
                <view class="toggle-item">
                    <text class="toggle-label">Organic options available</text>
                    <view class="switch-container">
                        <text class="switch-text">Yes</text>
                        <switch class="toggle-switch" :checked="organicOptions" @change="onOrganicChange" color="#333" />
                        <text class="switch-text">No</text>
                    </view>
                </view>
                <view class="toggle-item">
                    <text class="toggle-label">Recycled options available</text>
                    <view class="switch-container">
                        <text class="switch-text">Yes</text>
                        <switch class="toggle-switch" :checked="recycledOptions" @change="onRecycledChange" color="#333" />
                        <text class="switch-text">No</text>
                    </view>
                </view>
            </view>

			<button class="next-btn" @click="goToFactoryManufacturing">Next</button>
        </scroll-view>

        <AccountMenu
            v-if="showMenu"
            :style="{ '--anchor-top': '70px' }"
            @close="showMenu = false"
            @select="() => showMenu = false"
        />
	</view>
</template>

<script setup>
import TopBar from '@/components/TopBar.vue';
import AccountMenu from '@/components/AccountMenu.vue';
import { ref } from 'vue';

const goBack = () => uni.navigateBack();
const showMenu = ref(false);
const toggleMenu = () => { showMenu.value = !showMenu.value; };

// 材料数据
const plantFibers = ref([
    { name: 'Cotton', checked: true },
    { name: 'Linen / Flax', checked: false },
    { name: 'Hemp', checked: false },
    { name: 'Ramie', checked: false },
    { name: 'Other', checked: false }
]);

const animalFibers = ref([
    { name: 'Wool', checked: true },
    { name: 'Merino Wool', checked: true },
    { name: 'Cashmere', checked: false },
    { name: 'Mohair', checked: false },
    { name: 'Alpaca', checked: false },
    { name: 'Angora', checked: false },
    { name: 'Silk', checked: false },
    { name: 'Leather', checked: false }
]);

const regeneratedFibers = ref([
    { name: 'Viscose / Rayon', checked: true },
    { name: 'Modal', checked: true },
    { name: 'Lyocell (TENCEL™)', checked: true },
    { name: 'Acetate', checked: false },
    { name: 'Triacetate', checked: true },
    { name: 'Cupro', checked: false }
]);

const syntheticFibers = ref([
    { name: 'Polyester', checked: true },
    { name: 'Recycled Polyester (rPET)', checked: true },
    { name: 'Polyamide / Nylon', checked: false },
    { name: 'Acrylic', checked: false },
    { name: 'Elastane / Spandex (Lycra®)', checked: false },
    { name: 'Polypropylene (PP)', checked: false },
    { name: 'Polyethylene (PE, incl. UHMWPE)', checked: false },
    { name: 'PTT (Sorona®)', checked: false },
    { name: 'PTFE (ePTFE, Gore-Tex®)', checked: false },
    { name: 'Polyurethane (PU)', checked: false }
]);

const fillings = ref([
    { name: 'Duck Down', checked: true },
    { name: 'Goose Down', checked: true },
    { name: 'Duck Feather', checked: false },
    { name: 'Goose Feather', checked: false },
    { name: 'Polyester fill', checked: false },
    { name: 'Wool fill', checked: false },
    { name: 'Silk floss', checked: false },
    { name: 'Other', checked: false }
]);

const functionalMaterials = ref([
    { name: 'Nonwoven fabrics', checked: false },
    { name: 'Coated fabrics (PU, PVC)', checked: false },
    { name: 'Laminated / Composite fabrics', checked: false },
    { name: 'Rubber / Elastomer components', checked: false },
    { name: 'Metal fibers', checked: false }
]);

// 分类映射，避免使用 eval
const categoryMap = {
    plantFibers,
    animalFibers,
    regeneratedFibers,
    syntheticFibers,
    fillings,
    functionalMaterials
};

// 开关状态
const organicOptions = ref(true);
const recycledOptions = ref(true);

// 切换复选框状态
const toggleCheckbox = (category, itemName) => {
    const categoryData = categoryMap[category];
    if (!categoryData) return;
    const item = categoryData.value.find(item => item.name === itemName);
    if (item) {
        item.checked = !item.checked;
    }
};

// 开关变化处理
const onOrganicChange = (e) => {
    organicOptions.value = e.detail.value;
};

const onRecycledChange = (e) => {
    recycledOptions.value = e.detail.value;
};

const goToFactoryManufacturing = () => {
    uni.navigateTo({
        url: '/pages/factory/factory-manufacturing'
    });
};
</script>

<style lang="scss">
.factory-info-page { min-height: 100vh; background: linear-gradient(to bottom, #BFE7FF 0%, #EBFFE8 86%, #FFFFFF 100%); }
.form-scroll { margin-top: 60px; padding: 0 16px; box-sizing: border-box; }
.back-row { margin: 12px 0; width: 44px; height: 44px; display: flex; align-items: center; justify-content: center; }
.back-arrow-icon { width: 32px; height: 32px; }
.header { margin-bottom: 8px; }
.title { font-size: 22px; font-weight: 800; color: #0b1e2a; }
.subtitle { display: block; margin-top: 6px; color: #3b5560; font-size: 14px; }

.section-title { 
    font-size: 16px; 
    font-weight: 600; 
    color: #23323a; 
    margin: 16px 0 8px 0; 
    display: block; 
}
.section { 
    background: #fff; 
    border-radius: 12px; 
    padding: 16px; 
    margin-bottom: 12px; 
    box-shadow: 0 2px 8px rgba(0,0,0,.05); 
}
.checkbox-list { }
.checkbox-item { 
    display: flex; 
    align-items: center; 
    justify-content: space-between; 
    margin-bottom: 4px; 
    padding: 2px 0; 
}
.item-content {
    display: flex;
    align-items: center;
}
.bullet {
    color: #333;
    font-size: 16px;
    margin-right: 8px;
}
.checkbox { 
    width: 20px; 
    height: 20px; 
    border: 2px solid #333; 
    border-radius: 4px; 
    display: flex; 
    align-items: center; 
    justify-content: center; 
    background: #fff; 
}
.checkbox.checked { 
    background: #333; 
    border-color: #333; 
}
.checkmark { 
    color: #fff; 
    font-size: 12px; 
    font-weight: bold; 
}
.checkbox-label { 
    color: #333; 
    font-size: 14px; 
}

.toggle-section { 
    margin-bottom: 12px; 
}
.toggle-item { 
    display: flex; 
    justify-content: space-between; 
    align-items: center; 
    margin-bottom: 12px; 
}
.toggle-item:last-child { 
    margin-bottom: 0; 
}
.toggle-label { 
    color: #000; 
    font-size: 16px; 
    font-weight: 500;
}
.switch-container {
    display: flex;
    align-items: center;
    gap: 12px;
}
.switch-text {
    color: #000;
    font-size: 16px;
    font-weight: 500;
}
.toggle-switch { 
    transform: scale(0.8); 
}
/* 确保switch为黑色主题 */
.toggle-switch :deep(.uni-switch-input) {
    background-color: #333 !important;
    border-color: #333 !important;
}
.toggle-switch :deep(.uni-switch-input:before) {
    background-color: #333 !important;
}
.toggle-switch :deep(.uni-switch-input.uni-switch-input-checked) {
    background-color: #333 !important;
    border-color: #333 !important;
}
.toggle-switch :deep(.uni-switch-input.uni-switch-input-checked:before) {
    background-color: #fff !important;
}

.next-btn { 
    margin: 60px 8px 40px; 
    height: 44px; 
    background: #222; 
    color: #fff; 
    border-radius: 10px; 
    display: flex; 
    align-items: center; 
    justify-content: center; 
}
</style>
